package cn.tedu.ivos.vehicle.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleSaveParam;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos.vehicle.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class VehicleServiceImpl implements VehicleService {
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public List<VehicleVO> selectVehicle(VehicleQuery vehicleQuery) {
        log.debug("查询车辆业务:{}",vehicleQuery);
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        return list;
    }

    @Override
    public void saveVehicle(VehicleSaveParam vehicleSaveParam) {
        log.debug("新增车辆业务:{}",vehicleSaveParam);
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleSaveParam,vehicle);
        //判断该车牌号是否已经存在
        VehicleVO vehicleVO = vehicleMapper.selectVehicleByLicense(vehicle.getLicense());
        if(vehicle.getId() == null){//新增
            //如果根据前端传过来的车牌号查到了车,说明车牌号已存在,不能新增重复的车牌号,抛出业务层异常
            if(vehicleVO != null){
                throw new ServiceException(StatusCode.LICENSE_EXISTS);
            }
            vehicle.setStatus("1");//空闲1 占用2
            vehicle.setGeofenceBindStatus("0");//新增的车还未绑定围栏
            vehicle.setCreateTime(new Date());
            vehicleMapper.insert(vehicle);
        }else{
            //如果车牌号已存在,且此车牌号对应的车辆id不是现在要更新的这辆车id
            //也就是说,不能将当前待修改的车牌号改成数据库已存在的其它车的车牌号!
            if(vehicleVO != null && vehicleVO.getId() != vehicle.getId()){
                throw new ServiceException(StatusCode.LICENSE_EXISTS);
            }
            vehicle.setUpdateTime(new Date());
            vehicleMapper.update(vehicle);
        }
    }

    @Override
    public void deleteVehicle(Long id) {
        log.debug("删除车辆业务:{}",id);
        vehicleMapper.deleteById(id);
    }

    @Override
    public void unbindVehicle(Long vehicleId) {
        log.debug("解绑车辆业务:{}",vehicleId);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);//设置更新哪辆车
        vehicle.setGeofenceBindStatus("0");//设置车辆的围栏绑定状态为未绑定围栏
        /* 之前的SQL要不为null才拼接条件,而现在要故意置为特殊值null
        * 所以之前的SQL无法复用,需要新写一个! */
        vehicle.setGeofenceId(null);//将车辆之前绑定的围栏id恢复为null值
        vehicle.setUpdateTime(new Date());
        vehicleMapper.updateNullValue(vehicle);
    }

    @Override
    public void bindVehicle(Long geofenceId, Long vehicleId) {
        log.debug("绑定车辆业务:{},{}",geofenceId,vehicleId);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setGeofenceId(geofenceId);
        vehicle.setGeofenceBindStatus("1");//设置车辆的围栏绑定状态为已绑定围栏
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }
}
